package com.sunnada.btfvilg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunnada.btfvilg.common.exception.ServiceException;
import com.sunnada.btfvilg.entity.User;
import com.sunnada.btfvilg.mapper.UserMapper;
import com.sunnada.btfvilg.model.request.UserParamVo;
import com.sunnada.btfvilg.model.request.UserRegParamVo;
import com.sunnada.btfvilg.model.response.UserListVo;
import com.sunnada.btfvilg.model.response.UserVo;
import com.sunnada.btfvilg.model.response.WordVo;
import com.sunnada.btfvilg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * 服务实现类
 *
 * @author hcx
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public UserVo adminLogin(UserParamVo user) {
        if( "admin".equals(user.getUsername()) && "123123".equals(user.getPassword())){
            return new UserVo(null, "admin");
        }else{
            throw new ServiceException("用户名或密码错误");
        }
    }

    /**
     * 登录
     *
     * @param userParamVo
     * @return
     */
    @Override
    public UserVo login(UserParamVo userParamVo) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("uname", userParamVo.getUsername());
        User user = getOne(userQueryWrapper);
        if (user == null) {
            throw new ServiceException("用户不存在");
        }
        String inputPassword = userParamVo.getPassword();
        if (Objects.equals(inputPassword, user.getUpassword())) {
            return new UserVo(user.getUid(),user.getUname());
        } else {
            throw new ServiceException("用户名或密码错误");
        }
    }

    @Override
    public UserListVo getUserList(Integer page, Integer numPerPage) {
        UserListVo userList = new UserListVo();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        Integer count = userMapper.selectCount(userQueryWrapper);
        userList.setTotal(count);
        List<User> users = userMapper.getUserList((page-1)*numPerPage, numPerPage);
        userList.setUserList(users);
        userList.setPage(page);
        userList.setNumPerPage(numPerPage);
        return userList;
    }

    @Override
    public UserVo userRegister(UserRegParamVo userRge) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("uname", userRge.getUsername());
        Integer count = userMapper.selectCount(qw);
        if(count != 0 ){
            throw new ServiceException("用户名已存在");
        }
        User user = new User();
        user.setUid(UUID.randomUUID().toString().replaceAll("-",""));
        user.setUname(userRge.getUsername());
        user.setUpassword(userRge.getPassword());
        user.setUphone(userRge.getUphone());
        user.setUsex(userRge.getUsex());
        userMapper.insert(user);
        UserVo reg = new UserVo(user.getUid(),user.getUname());
        return reg;
    }

    @Override
    public void delUser(String uid) {
        userMapper.deleteById(uid);
    }

    @Override
    public void updateUser(UserRegParamVo userRge) {
        User user = new User();
        user.setUid(userRge.getUid());
        user.setUname(userRge.getUsername());
        user.setUpassword(userRge.getPassword());
        user.setUphone(userRge.getUphone());
        user.setUsex(userRge.getUsex());
        userMapper.updateById(user);
    }

    @Override
    public User getUserById(String uid) {
        return userMapper.selectById(uid);
    }

}
